 fs = 10e3;
if ~exist('stft.wav','file')==1
 t = 0:1/fs:2;
 x = vco(sin(2*pi*t),[0.1 0.4]*fs,fs);
 audiowrite("stft.wav",x,fs)
else
 fs,x=audioread("stft.wav")
end

win=hann(256,'periodic')
writematrix(transpose(round(win,4)),"stft-win.txt",'delimiter','tab')

[s,f,t] = stft(x,fs,Window=win,OverlapLength=220,FFTLength=512,FrequencyRange='onesided');
figure
sdb = mag2db(abs(s));
mesh(t,f/1000,sdb);
saveas(gcf,"stft-mesh1.jpg")

cc = max(sdb(:))+[-60 0];
%Get current axes
ax = gca;
ax.CLim = cc;
view(2)
colorbar
saveas(gcf,"stft-1.jpg")

format short
writematrix(transpose(round(s,4)),"stft-s1.txt",'delimiter','tab')
writematrix(transpose(f),"stft-f1.txt",'delimiter','tab')
writematrix(transpose(t),"stft-t1.txt",'delimiter','tab')

[s,f,t] = stft(x,fs,Window=win,OverlapLength=220,FFTLength=512,FrequencyRange='twosided');
figure
sdb = mag2db(abs(s));
mesh(t,f/1000,sdb);
saveas(gcf,"stft-mesh2.jpg")
cc = max(sdb(:))+[-60 0];
%Get current axes
ax = gca;
ax.CLim = cc;
view(2)
colorbar
saveas(gcf,"stft-2.jpg")

writematrix(transpose(round(s,4)),"stft-s2.txt",'delimiter','tab')
writematrix(transpose(f),"stft-f2.txt",'delimiter','tab')
writematrix(transpose(t),"stft-t2.txt",'delimiter','tab')


[s,f,t] = stft(x,fs,Window=win,OverlapLength=220,FFTLength=512);
figure
sdb = mag2db(abs(s));
mesh(t,f/1000,sdb);
saveas(gcf,"stft-mesh3.jpg")
cc = max(sdb(:))+[-60 0];
%Get current axes
ax = gca;
ax.CLim = cc;
view(2)
colorbar
saveas(gcf,"stft-3.jpg")


writematrix(transpose(round(s,4)),"stft-s3.txt",'delimiter','tab')
writematrix(transpose(f),"stft-f3.txt",'delimiter','tab')
writematrix(transpose(t),"stft-t3.txt",'delimiter','tab')
